<?php

/*

CREATE TABLE IF NOT EXISTS `think_subscribe_member` (
  `member_id` int(11) NOT NULL,
  `site_id` int(11) NOT NULL,
  `datetime` varchar(30) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

*/

class Bonus{
	public $site_id = 0;
	public $member_id = 0;
	public $config = array();
	public $configIntegrals = array();
	public $memberdb;
	
	
	//构造函数
	public function Bonus($site_id = 0, $member_id = 0){
		if($site_id == 0){
			$site_id = defined('__ADMINSITEID__')?__ADMINSITEID__:__SITEID__;
		}
		
		$this->site_id = $site_id;
		$this -> memberdb = D('Member');
		$db = M('Subscribe');
		
		$ret = $db -> where(array('site_id' => $site_id,'classname' => 'Bonus')) -> find();
		$config = unserialize($ret['value']);
		$this -> config = $config['level'];
		$this->configIntegrals = $config['Integrals'];
	    $this->member_id = $member_id;
	}
	
	//创建设置项表单
	public function createform(){
		
		$db = M('Subscribe');
		$ret = array('','','','');
		$ret = $db -> where(array('site_id' => $this->site_id,'classname' => 'Bonus')) -> find();
		$ret = unserialize($ret['value']);
		$html = '<style>#Wxpay td{padding:5px;}</style>
		<table id="Wxpay" border="1" cellspacing="0" cellpadding="5" width="100%"><tr>
    <td align="center" valign="middle"><input type="hidden" name="classname" value="Bonus">级别</td>
    <td align="center">送积分</td>
    <td align="center">送现金</td>
  </tr> 
 <tr>
    <td align="center" valign="middle"><input type="hidden" name="classname" value="Bonus">关注者送</td>
    <td align="center"><input type="text" name="Integrals[]" value="'.$ret['Integrals'][0].'" /></td>
    <td align="center"><input type="text" name="level[]" value="'.$ret['level'][0].'" />元</td>
  </tr> 
  <tr>
    <td align="center" valign="middle">第一级推荐人送</td>
    <td align="center"><input type="text" name="Integrals[]" value="'.$ret['Integrals'][1].'" /></td>
    <td align="center"><input type="text" name="level[]" value="'.$ret['level'][1].'" />元</td>
  </tr> 
  <tr>
    <td align="center" valign="middle">第二级推荐人送</td>
    <td align="center"><input type="text" name="Integrals[]" value="'.$ret['Integrals'][2].'" /></td>
    <td align="center"><input type="text" name="level[]" value="'.$ret['level'][2].'" />元</td>
  </tr>
  <tr>
    <td align="center" valign="middle">第三级推荐人送</td>
    <td align="center"><input type="text" name="Integrals[]" value="'.$ret['Integrals'][3].'" /></td>
    <td align="center"><input type="text" name="level[]" value="'.$ret['level'][3].'" />元</td>
  </tr>
  
</table>';

        return $html;
	}
	
	//保存设置项
	public function saveform(){
		if(isset($_POST['level'])){
			$data['classname'] = 'Bonus';
			$data['value'] = serialize($_POST);
			$data['site_id'] = $this->site_id;
			$db = M('Subscribe');
			//dump($data);
			$ret = $db -> where(array('site_id' => $data['site_id'],'classname' => $data['classname'])) -> find();
			if($ret){
				$saveret = $db -> where(array('site_id' => $data['site_id'],'classname' => $data['classname'])) -> data(array('value' => $data['value'])) -> save();
			}else{
				$saveret = $db -> data($data) -> add(); 
			}
			if($saveret){
				return true; 
			}else{
				return false;
			}
			
		}
	}
	
	public function getUserArr($pid=0, $arr = array(), $deep = 0){
		$re=$this->memberdb->where(array('id'=>$pid,'site_id'=>$this->site_id))->find();
		//8级返回 
		if($deep == 4){return $arr;}
		if(!is_array($re)){
			return $arr;
		}else{
			$re['deep'] = $deep + 1;
			$arr[] = $re;
			$deep ++;
			return $this->getUserArr($re['pid'], $arr, $deep);
		}
	}
	
	
	
	
	//执行
	public function init(){
		$db = D('Member_account_log');
	    if(!$this->subscritbe()){
			$userlist = $this->getUserArr($this->member_id);
			foreach($userlist as $k => $v){
				$db -> change($this->config[$k], $v['id'], '【'.$userlist[0]['nickname'].'】关注赠送！');
				
				import("@.ORG.Integrals.Integral"); 
			    $Integral = new Integral($v['id']);
			    $Integral->giveIntegral($this->configIntegrals[$k]);
			}
			return true;
		}
		return false;
	}
	
	
	//是否关注过一次
	public function subscritbe(){
		$issubscritbe = M('Subscribe_member') -> where(array('member_id' => $this->member_id, 'site_id' => $this -> site_id)) -> count();
		if($issubscritbe > 0){
			
			return true;
		}
		M('Subscribe_member')-> data(array('member_id' => $this->member_id, 'site_id' => $this->site_id, 'datetime' => time())) -> add();
		return false;
	}
	
	
}